#ifndef TABSSCROLLAREA_H
#define TABSSCROLLAREA_H

#include <QScrollArea>

#include "dragabletab.h"

class QHBoxLayout;
class DroppableWidget;
class PagesWidget;

/*
    继承了 QScrollArea GUI类
    整体功能:
        内置了dragableTab，作为tabs的集合
    具体功能：
        1、继承了scrollArea相关滚动的功能
        2、实现了整个scrollArea拖拽功能和相关信号
        3、集成了对单个tab的处理
*/

class TabsScrollArea : public QScrollArea
{
    Q_OBJECT
public:
    explicit TabsScrollArea(QWidget* parent = nullptr);
    ~TabsScrollArea() override;

    std::pair<int, int> Id();
    void SetId(int dId, int pId);

    void SetParentPagesWidget(PagesWidget* pagesWidget);
    PagesWidget* ParentPagesWidget() const;

    std::pair<DragableTab*, QWidget*> Page(int index) const;
    std::pair<DragableTab*, QWidget*> Page(const DragableTab *tab) const;

    int PagesCount() const;
    int CurrentIndex() const;
    int PageAt(const DragableTab *tab) const;

    void AddPage(DragableTab *tab, QWidget *content);
    void AddPage(QString titleLabel, QWidget *content, DragableTab::TabType tabType=DragableTab::TabType::DEFAULT);
    void InsertPage(int index, DragableTab *tab, QWidget *content);
    void InsertPage(int index, QString titleLabel, DragableTab::TabType tabType, QWidget *content);
    std::pair<QWidget*, QWidget*> SwapPage(int from, int to);
    std::pair<DragableTab*, QWidget*> ReplacePage(DragableTab* fromTab, DragableTab* toTab, QWidget* toContent);


    int CurrentPageIndex() const;
    std::pair<DragableTab*, QWidget*> CurrentPage() const;

    std::pair<DragableTab*, QWidget*> RemovePage(int index);
    std::pair<DragableTab*, QWidget*> RemovePage(const DragableTab *tab);
    void Clear();

    void setCurrentPage(DragableTab *tab);
    void setCurrentIndex(int index);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void dragEnterEvent(QDragEnterEvent *event) override;

    void closeEvent(QCloseEvent *event) override;
    void SetDroppableWidget(DroppableWidget* widget);

    bool SinglePageDropIn(QPoint dropPos, std::tuple<int, int, int> dragIndex, std::tuple<int, int, int> dropIndex);
    bool PagesDropIn(QPoint dropPos, std::pair<int, int> dragIndex, std::tuple<int, int, int> dropIndex);

private:
    void Init();
    void Finalize() const;

    // scrollArea 整个的拖拽过程
    QPoint mDragStartPos {-1, -1};
    bool mDragReady {false};
    QHBoxLayout* mTabsLayout {nullptr};
    QWidget* tabsWidget {nullptr};

    // tab相关
    void TabConnect(DragableTab *tab) const;
    void TabDisconnect(DragableTab *tab) const;
    void ChangeCurrentPage(DragableTab* tab);

    std::pair<int, int> mId {-1, -1};
    int mCurrentIndex {-1};
    int mPagesCount {0};
    DroppableWidget* mDroppableWidget;
    PagesWidget* mParentPagesWidget;
    std::pair<DragableTab*, QWidget*> mCurrentPage {nullptr, nullptr};
    QList<std::pair<DragableTab*, QWidget*>> mPages;

public slots:
    void RemoveDragOutPage(std::tuple<int, int, int> dragIndex);

private slots:
    void NotifyParentWidgetToFlatPage(std::tuple<int, int, int> index);
    void CloseTabFromButton();
    void ClickTab();

signals:
    // void CurrentPageChanged(TabsScrollArea* pagesWidget);
    void NeedToFlatPage(int index);
    void NeedToFlatAllPages();
    void closed();

    // new, 需要提供tabsScrollArea与pagesWidget的联系
    void pageAdded(QWidget* content);
    void pageRemoved(QWidget* content);
    void currentPageChanged(QWidget* content);

    // 暂时，方便调试
    friend class DropManager;
    friend class DroppableWidget;
};

#endif // TABSSCROLLAREA_H
